package com.aiit.service.impl;

import com.aiit.constant.StatusConstant;
import com.aiit.context.BaseContext;
import com.aiit.dto.EmployeePageQueryDTO;
import com.aiit.dto.TeaCupDTO;
import com.aiit.entity.TeaCup;
import com.aiit.mapper.TeaCupMapper;
import com.aiit.result.PageResult;
import com.aiit.service.TeaCupService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.annotation.WebServlet;
import java.time.LocalDateTime;
import java.util.List;

@Service
@WebServlet("/verifyCode")
public class TeaCupServiceImpl implements TeaCupService {

    @Autowired
    private TeaCupMapper teaCupMapper;

    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());

        Page<TeaCup> teaCups = teaCupMapper.pageQuery(employeePageQueryDTO);

        long total = teaCups.getTotal();
        List<TeaCup> records = teaCups.getResult();

        return new PageResult(total, records);
    }


    public void update(TeaCupDTO teaCupDTO) {
        TeaCup teaCup = new TeaCup();
        BeanUtils.copyProperties(teaCupDTO, teaCup);

        teaCup.setUpdateTime(LocalDateTime.now());
        teaCup.setUpdateUser(BaseContext.getCurrentId());

        teaCupMapper.update(teaCup);
    }


    public void save(TeaCupDTO teaCupDTO) {
        System.out.println("当前线程的id:" + Thread.currentThread().getId());
        TeaCup teaCup = new TeaCup();

        //对象属性拷贝
        BeanUtils.copyProperties(teaCupDTO, teaCup);

        teaCup.setStatus(StatusConstant.ENABLE);

        teaCup.setCreateTime(LocalDateTime.now());
        teaCup.setUpdateTime(LocalDateTime.now());

        //设置当前记录创建人id和修改人的id
        // TODO 后期需要改为当前登录用户的id
        teaCup.setCreateUser(BaseContext.getCurrentId());
        teaCup.setUpdateUser(BaseContext.getCurrentId());

        teaCupMapper.insert(teaCup);
    }


    @Override
    public void deleteTea(String name) {
        teaCupMapper.deleteById(name);
    }

    @Override
    public List<TeaCup> list(TeaCup teaCup) {
        List<TeaCup> list = teaCupMapper.list(teaCup);
        return list;
    }
}



